# @Desc    : 体脂率计算
# @Author  : meihongliang  
# @Time    : 2025-01-12 17:14
from datetime import time, datetime


def get_body_fat_percentage(waistline, weight):
    a = waistline * 0.74
    b = (weight * 0.082) + 44.74
    total_weight = a - b
    body_fat_per = (total_weight / weight) * 100
    per = round(body_fat_per, 2)
    return per


def tzl_with_bmi(bmi, age, sex):
    # 体脂率：1.2×BMI+0.23×年龄-5.4-10.8×性别（男为1，女为0）
    tzl_value = (1.2 * bmi) + (0.23 * age) - 5.4 - (10.8 * sex)
    return round(tzl_value, 2)


def bmi(height, weight):
    # height=身高，米m
    # BMI=体重（公斤）÷（身高×身高）（米）
    bmi_value = weight / (height * height)
    return round(bmi_value, 2)


def bim_cn(bmi):
    if bmi < 18.5:
        return "体重较轻"
    elif 18.5 <= bmi <= 24:
        return "正常范围"
    elif 24 <= bmi <= 27:
        return "体重过重"
    elif 27 <= bmi <= 30:
        return "轻度肥胖"
    elif 30 <= bmi <= 35:
        return "中度肥胖"
    elif 35 <= bmi:
        return "重度肥胖"


def jcdxl(weight, height, age):
    """计算基础代谢率
    男性: BMR = 10 × 体重(kg) + 6.25 × 身高(cm) - 5 × 年龄(年) + 5
    女性: BMR = 10 × 体重(kg) + 6.25 × 身高(cm) - 5 × 年龄(年) - 161
    """
    jcdxl_value = (10 * weight) + (6.25 * height * 100) - (5 * age)
    return round(jcdxl_value, 2)


def main():
    # height_str = input('请输入您的身高(m): ')
    weight_str = input('请输入您的体重(斤): ')
    # waistline_str = input('请输入您的腰围(cm): ')
    # 1.62 74 91
    # height_str = '1.58'  # 身高m
    height_str = '1.62'  # 身高m
    # weight_str = weight_str / 2  # 体重kg
    waistline_str = '91'  # 腰围cm
    f = open('body_info.txt', 'a+')
    f.write(f'==体脂率高于10%以上脂肪就会盖住腹肌=={datetime.now().strftime("%Y%m%d_%H%M")}\n')
    waistline = float(waistline_str)
    weight = float(weight_str) / 2
    height = float(height_str)

    # tzl = get_body_fat_percentage(waistline, weight)
    bmi_value = bmi(height, weight)
    tzl = tzl_with_bmi(bmi_value, 29, 1)
    bim_cd = bim_cn(bmi_value)
    # f.write('身高\t\t体重\t\t\t腰围\t\t\t体脂率\t\tBMI\n')
    f.write('身高\t\t体重\t\t\t体脂率(<10%)\t\tBMI(18.5~23.9)\n')
    f.write(f'{height}(m)\t{weight}(kg)\t{tzl}%\t\t{bmi_value}%-{bim_cd}\n')
    print('finished')
    print(f'基础代谢率={jcdxl(weight, height, age=29)}')  # 1550.5


main()
